/*
 * @Descripttion: 
 * @version: 
 * @Author: liyuhang
 * @Date: 2024-08-30 10:27:55
 */
//  本来应该放在35行p标签里    <van-popover width="550" placement="bottom-start" theme="dark" v-model="showPopover" trigger="click" :actions="actions" >
// <template #reference>
// <van-icon slot="reference" name="info-o"/>
// </template>
// </van-popover>
Vue.component('wode-page', {
    props: ['dot'],
    template: '\
        <div class="wo_box">\
        <van-popup :close-on-click-overlay="false" class="vanPop" v-model="voiceShow" closeable position="bottom" :style=\'{height: "39%",borderRadius:"5% 5% 0 0",left:0,bottom:0}\'> \
            <div class="popTitle">声音切换 </div>\
            <van-radio-group v-model="vioceBtn">\
                <van-cell-group>\
                    <van-cell v-for="(item,index) in voiceList"  :key="index" :title="item.title" clickable @click="vioceBtn = item.id">\
                        <template #right-icon>\
                            <van-radio :name="item.id" />\
                        </template>\
                    </van-cell>\
                </van-cell-group>\
            </van-radio-group>\
        </van-popup>\
        <div class="header_tit">\
        <van-nav-bar title="个人中心"></van-nav-bar>\
        <div class="userName">\
            <p style="font-weight: bold;">{{getEmailName}}</p>\
            <p v-if=\'getEmailID=="null"\'></p>\
            <p v-else>{{getEmailID}}</p>\
        </div>\
        </div>\
         <div class="wo-list">\
            <div class="wo_tab" v-for="(item,index) in filteredTabList">\
              <template v-if="item.id == 15">\
            <div class="wo_tab_box"> <p @click="usageClick(item)" class="tabs" >\
            {{item.name}}\
            </p>\
           </div>\
              <van-switch size="18px" @change="switchChange" v-model="checked" /> </van-switch>\
             </template>\
             <template v-else-if="item.id == 7">\
            <div class="wo_tab_box"> <p class="tabs" v-text="item.name"></p><div class="wo_tab_icon">\
            <van-popover width="550" placement="bottom-start" theme="dark" v-model="showPopover" trigger="click" :actions="actions" >\
          <template #reference>\
              <van-icon slot="reference" name="info-o"/>\
          </template>\
         </van-popover>\
           </div>\
           </div>\
              <van-switch size="18px" v-model="checkeds" /> </van-switch>\
             </template>\
                   <template v-else>\
                    <template v-if="item.id == 11">\
                    <p class="tabs" @click="usageClick(item)" v-text="item.name"></p><van-icon :dot="dot" > <van-icon>\
                   </template>\
                   <template v-else>\
                    <p class="tabs" @click="usageClick(item)" v-text="item.name"></p>\
                    </template>\
                       <img class="tab_img" src="../image/right.png" />\
                   </template>\
            </div>\
        </div>\
      </div>\
        ',
    
    data() {
        return {
            tabList: [{
                    id: 1,
                    name: "配置内网邮箱",
                },
                // {
                //     id: 7,
                //     name: "模型选择悬浮框",

                // },
                {
                    id: 12,
                    name: "设置日程助理"
                },
                // {
                //     id: 6,
                //     name: "应用看板",

                // },
                // {
                //     id: 2,
                //     name: "使用统计",
                // },
                {
                    id: 8,
                    name: "指导中心",
                },
                {
                    id: 9,
                    name: "调研问卷",
                },
                {
                    id: 5,
                    name: "问题反馈",

                },
                // {
                //     id: 10,
                //     name: "声音切换",

                // },
                {
                    id: 11,
                    name: "消息通知",

                },
               
                {
                    id: 4,
                    name: "关于",

                },
               
              
            ],
            radio: localStorage.getItem("largeModel") == "BAIDU" ? "2" : "1",
            vioceBtn: "",
            langList: [{
                    id: "1",
                    title: '光明大模型A',
                    content: "ALI"
                },
                {
                    id: "2",
                    title: '光明大模型B',
                    content: 'BAIDU'

                },
            ],
            voiceList: [{
                    id: "1",
                    title: '婷婷',
                    content: "panting"

                },
                {
                    id: "2",
                    title: '思婷',
                    content: 'siting'

                },
                {
                    id: "3",
                    title: '小关',
                    content: 'xiaoguan_CTS'

                },
                {
                    id: "4",
                    title: '小忠',
                    content: 'xiaozhong'

                },
            ],
            popShow: false,
            voiceShow: false,
            // getEmailID: "",
            getEmailID: sessionStorage.getItem("toMail"),
            getEmailName: "",
            langAct: '',
            doot:true,
            checkeds: false,// 悬浮窗是否显示
            checked:localStorage.getItem("showBtn") == "true" ? true : false,// 悬浮窗是否显示
            showPopover: false,
           // 通过 actions 属性来定义菜单选项
         actions: [{ text: '仅支持模型切换的应用可开启悬浮窗' }],
        //  showSuspension: false, 
        }
    },
    computed: {
        filteredTabList() {
          // 根据返回值过滤 "设置日程助理"
          return this.tabList.filter(tab => {
            if (tab.id === 12) {
              return travelAssistance('12', '12-trip'); // 只对 id=12 的选项进行判断
            }
            return true; // 其他选项正常显示
          });
        }
      },
    watch: {
        // radio: {
        //     handler(t) {
        //         this.langList.find((item) => {
        //             if (item.id == t) {
        //                 localStorage.setItem("largeModel", item.content)
        //                 this.tabList.forEach((item) => {
        //                     if (item.id == 7) item.name = localStorage.getItem("largeModel") == "ALI" ? "光明大模型A" : "光明大模型B"
        //                 })
        //                 this.popShow = false
        //             }

        //         })
        //     },
        //     immediate: true
        // },
        checked: {
            handler(t) {
                // localStorage.setItem("showSuspension", this.checked)
            },
            immediate: true
        },
        vioceBtn: {
            handler(t) {
                this.voiceList.find((item) => {
                    if (item.id == t) {
                        localStorage.setItem("nativeVoiceName", item.content)
                        // this.tabList.forEach((item) =>{
                        //   if(item.id == 7) item.name = localStorage.getItem("largeModel") == "ALI" ? "光明大模型A" : "光明大模型B"
                        // })
                        this.voiceShow = false
                    }

                })
            },
            immediate: true
        },
    },
    created() {
        this.init()

    },
    methods: {
        switchChange(val) {
            this.checked = val
            localStorage.setItem("showBtn", val)

        },
        init() {
            console.log(isflageIOX,8878878)
             // 如果不是苹果设备，在"模型选择悬浮框"后添加新项
       if (!isflageIOX) {
        // 找到id为7的元素索引
        const index = this.tabList.findIndex(item => item.id === 12);
        
        // 如果找到则插入新项
        if (index !== -1) {
            // 在索引+1的位置插入新对象
            this.tabList.splice(index + 1, 0, {
                id: 15,  // 注意：这里原数组已有id为15的项，可能需要调整
                name: "选择默认路径"
            });
        }
        console.log(this.tabList,877)
    }
    
    // 将处理后的数组赋值给组件数据
    //    this.tabList = tabList;
            const voiceMap = {
                "siting": "2",
                "xiaoguan_CTS": "3",
                "xiaozhong": "4"
            };

            this.vioceBtn = voiceMap[localStorage.getItem("nativeVoiceName")] || "1";
            // switch (localStorage.getItem("nativeVoiceName")) {
            //     case "siting":
            //         this.vioceBtn = "2"
            //         break;
            //     case "xiaoguan_CTS":
            //         this.vioceBtn = "3"
            //         break;
            //     case "xiaozhong":
            //         this.vioceBtn = "4"
            //         break;

            //     default:
            //         this.vioceBtn = "1"
            //         break;
            // }
            let newUserName
            let userNameString = sm2.doDecrypt(sessionStorage.getItem("userName"), sessionStorage.getItem("fprk"), 1)
            let userNameList = userNameString.split(",");
            if (sm3(userNameList[0]) == userNameList[1]) {
                newUserName = userNameList[0]
                this.getEmailName = newUserName
                if (isFlagOne) {
                    this.getEmailName = noPassByName(this.getEmailName)
                } else {
                    this.getEmailName = newUserName
                }
            }

        },
       
        getList() {
            if (sessionStorage.getItem("iscp_port")) {
                iscp_port = sessionStorage.getItem("iscp_port");
            } else {
                iscp_port = ""
            }
            let data = {
                "cId": "caozuoshouce",
                "timestamp": new Date().getTime() + "",
            };
            data = JSON.stringify(data)
            let sign = sm3(data);
            let AuthenticationSession = sessionStorage.getItem("Authentication");
            $.ajax({
                url: localUrlLink + iscp_port + "/tjAiAssistant/voice/queryOperatingManualInfo",
                type: "POST",
                async: false, //是否同步，false表示同步请求
                dataType: "json",
                data: data,
                headers: {
                    "Content-Type": "application/json;charset=UTF-8",
                    Authentication: AuthenticationSession,
                    sign: sign
                },
                success: (res, txt, xhr) => {
                    if (sm3(res.resultValueStr) == xhr.getResponseHeader("sign")) {
                        let Authentication = xhr.getResponseHeader("Authentication")
                        sessionStorage.setItem("Authentication", Authentication);
                        if (res.code == 200) {
                            var file = {
                                name: res.resultValue.name,
                                id: res.resultValue.id,
                                size: res.resultValue.size,
                            }
                            this.myPreviewFile(file)
                        } else {
                            mui.toast("未获取到文件信息", {
                                duration: 'long',
                                type: 'div'
                            })
                        }
                    } else {
                        mui.toast('完整性校验失败', {
                            duration: 'long',
                            type: 'div'
                        })
                    }
                },
                error: (e) => {
                    mui.toast('请求异常,请稍后再试', {
                        duration: 'long',
                        type: 'div'
                    })
                },
            });
        },
        /**
         * 预览文件的方法
         * 该方法会根据传入的文件信息生成下载链接，并调用微信的 `previewFile` 接口进行文件预览
         * @param {Object} file - 文件信息对象，包含文件的 id、name、size 等属性
         * @param {string} file.id - 文件的唯一标识
         * @param {string} [file.name] - 文件的名称
         * @param {number} [file.size] - 文件的大小
         */
        myPreviewFile(file) {
            console.log(88, file)
            let downUrl = localUrlLink + iscp_port + '/tjAiAssistant/common/download';
            downUrl += '?id=' + file.id;
            let Authentication = sessionStorage.getItem("Authentication");
            let timestamp = new Date().getTime() + "";
            let sign = sm3(timestamp + '&' + file.id)
            downUrl += '&Authentication=' + Authentication + '&timestamp=' + timestamp + '&sign=' + sign;
            console.log(778)
            wx.invoke('previewFile', {
                url: downUrl,
                isLocalUrl: 0,
                name: file.name || file.fileName,
                size: file.size || file.fileSize,
                hidePreviewMenuList: []
            });
        },
        home() {
            $(location).attr("href", "Home.html");
        },
        // 封装跳转页面的函数
        navigateToPage(url) {
            $(location).attr("href", url);
        },

       /**
         * 确认重置内网邮箱的函数。
         * 如果传入的邮箱 ID 存在，则弹出确认对话框询问用户是否要重置邮箱；
         * 如果邮箱 ID 不存在，则直接跳转到注册页面。
         * @param {string} getEmailID - 当前设置的内网邮箱 ID
         */
        confirmResetEmail(getEmailID) {
            if (getEmailID) {
                var btnArray = ["取消", "确定"];
                mui.confirm(
                    "您已设置成功内网邮箱，确定要重置邮箱吗？",
                    "消息提醒",
                    btnArray,
                    (e) => {
                        if (e.index === 1) {
                            this.navigateToPage("zhu_ce.html");
                        }
                    }
                );
            } else {
                this.navigateToPage("zhu_ce.html");
            }
        },


        /**
         * 根据传入的 item 对象的 id 属性执行相应的处理函数
         * @param {Object} item - 包含 id 属性的对象，用于映射处理函数
         */
        usageClick(item) {
            // 定义一个对象来映射 item.id 到对应的处理函数
            const actionMap = {
                1: () => this.confirmResetEmail(this.getEmailID),
                2: () => this.navigateToPage("usageStatistics.html"),
                5: () => this.navigateToPage("problemFeedback.html"),
                6: () => this.navigateToPage("yr/dataBoard/index.html"),
                9: () => this.navigateToPage("quesTionnaire.html"),
                8: () => this.navigateToPage("yr/guidance/index.html"),
                // 8: () => this.getList(),
                7: () =>  false,
                10: () => this.voiceShow = true,
                11: () => this.navigateToPage("yr/message/index.html"),
                12: () => this.navigateToPage("yr/tripAgent/index.html"),
                15: () => this.navigateToPage("defaultPath.html"),
                default: () => {
                    this.navigateToPage("about.html");
                    addOperator(4, 1, "关于", 1, 1, "跳转页面");
                }
            }
            const action = actionMap[item.id] || actionMap.default;
            action();
        },


        // usageClick(item) {
        //     switch (item.id) {
        //         case 1:
        //             if (this.getEmailID) {
        //                 var btnArray = ["取消", "确定"];
        //                 mui.confirm(
        //                     "您已设置成功内网邮箱，确定要重置邮箱吗？",
        //                     "消息提醒",
        //                     btnArray,
        //                     (e) => {
        //                         if (e.index == 1) {
        //                             $(location).attr("href", "zhu_ce.html");
        //                         }
        //                     }
        //                 );
        //             } else {
        //                 $(location).attr("href", "zhu_ce.html");
        //             }

        //             break;
        //         case 2:
        //             $(location).attr("href", "usageStatistics.html");
        //             break;
        //         case 5:
        //         $(location).attr("href", "problemFeedback.html");
        //             break;
        //         case 6:
        //         $(location).attr("href", "yr/dataBoard/index.html");
        //             break;
        //         case 9:
        //         $(location).attr("href", "quesTionnaire.html");
        //             break;
        //         case 8:
        //             this.getList()
        //         // $(location).attr("href", "userManual.html");
        //             break;
        //         case 7:
        //         this.popShow = true
        //             break;
        //         case 10:
        //         this.voiceShow = true
        //             break;
        //         default:
        //             $(location).attr("href", "about.html");
        //             addOperator(4, 1, "关于", 1, 1, "跳转页面");
        //     }
        // },

    }
});